package com.catazy.jetpackstudy.act2.chapter4.contentprovider;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.catazy.jetpackstudy.R;

/**
 * @作者: Tao
 * @日期: 2025/2/8
 * @时间: 10:42
 * @描述:
 */
public class ProviderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_manager);
        // 对书数据库操作
        Uri bookUri = Uri.parse(BookProvider.BOOK_URI_CONTENT);
        ContentValues values = new ContentValues();
        values.put("_id", 6);
        values.put("name", "python");
        getContentResolver().insert(bookUri, values);
        Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
        if (bookCursor != null) {
            while (bookCursor.moveToNext()) {
                Log.d("ProviderActivity", "book id: " + bookCursor.getInt(0) + ", name: " + bookCursor.getString(1));
            }
            bookCursor.close();
        }

        // 对人员数据库操作
        Uri userUri = Uri.parse(BookProvider.USER_URI_CONTENT);
        Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "age"}, null, null, null);
        if (userCursor != null) {
            while (userCursor.moveToNext()) {
                Log.d("ProviderActivity", "user id: " + userCursor.getInt(0) + ", name: " + userCursor.getString(1) + ", age: " + userCursor.getInt(2));
            }
            userCursor.close();
        }


    }
}
